home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (c) 1983 Regents of the University of California.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms are permitted
- * provided that this notice is preserved and that due credit is given
- * to the University of California at Berkeley. The name of the University
- * may not be used to endorse or promote products derived from this
- * software without specific prior written permission. This software
- * is provided ``as is'' without express or implied warranty.
- */
-
- #if defined(LIBC_SCCS) && !defined(lint)
- static char sccsid[] = "@(#)getnetent.c 5.4 (Berkeley) 3/7/88";
- #endif /* LIBC_SCCS and not lint */
-
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netdb.h>
- #include <ctype.h>
-
- #define MAXALIASES 35
-
- static char NETDB[] = "/etc/networks";
- static FILE *netf = NULL;
- static char line[BUFSIZ+1];
- static struct netent net;
- static char *net_aliases[MAXALIASES];
- int _net_stayopen;
- static char *any();
-
- extern u_long inet_network();
-
- setnetent(f)
- int f;
- {
- if (netf == NULL)
- netf = fopen(NETDB, "r" );
- else
- rewind(netf);
- _net_stayopen |= f;
- }
-
- endnetent()
- {
- if (netf) {
- fclose(netf);
- netf = NULL;
- }
- _net_stayopen = 0;
- }
-
- struct netent *
- getnetent()
- {
- char *p;
- register char *cp, **q;
-
- if (netf == NULL && (netf = fopen(NETDB, "r" )) == NULL)
- return (NULL);
- again:
- p = fgets(line, BUFSIZ, netf);
- if (p == NULL)
- return (NULL);
- if (*p == '#')
- goto again;
- cp = any(p, "#\n");
- if (cp == NULL)
- goto again;
- *cp = '\0';
- net.n_name = p;
- cp = any(p, " \t");
- if (cp == NULL)
- goto again;
- *cp++ = '\0';
- while (*cp == ' ' || *cp == '\t')
- cp++;
- p = any(cp, " \t");
- if (p != NULL)
- *p++ = '\0';
- net.n_net = inet_network(cp);
- net.n_addrtype = AF_INET;
- q = net.n_aliases = net_aliases;
- if (p != NULL)
- cp = p;
- while (cp && *cp) {
- if (*cp == ' ' || *cp == '\t') {
- cp++;
- continue;
- }
- if (q < &net_aliases[MAXALIASES - 1])
- *q++ = cp;
- cp = any(cp, " \t");
- if (cp != NULL)
- *cp++ = '\0';
- }
- *q = NULL;
- return (&net);
- }
-
- static char *
- any(cp, match)
- register char *cp;
- char *match;
- {
- register char *mp, c;
-
- while (c = *cp) {
- for (mp = match; *mp; mp++)
- if (*mp == c)
- return (cp);
- cp++;
- }
- return ((char *)0);
- }
-